<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\api\energy;

use think\App;
use app\controller\api\Base;
use app\validate\energy\EnergySharedPlanValidate;
use app\common\repositories\energy\EnergyMeterRepository;
use app\common\repositories\energy\EnergySharedPlanRepository;


class EnergySharedPlan extends Base
{
    protected $repository;

    public function __construct(App $app, EnergySharedPlanRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    public function list()
    {
        $where = $this->request->param([
            'keywords' => '',
            'property_id' => '',
        ]);
        [$page, $limit] = $this->getPage();
        $where['add_uid'] = $this->request->userId();
        $res = $this->repository->getApiList($where, $page, $limit, $this->request->propertyId);
        return $this->success($res);
    }

    public function add()
    {
        $param = $this->request->param([
            'plan_name' => '',
            'property_id' => '',
            'plan_type' => '',
            'is_type' => '',
            'energy_id' => '',
            'energy_type' => '',
            'remark' => '',
        ]);
        validate(EnergySharedPlanValidate::class)->scene('add')->check($param);
        /** * @var EnergyMeterRepository $energyMeterRepository */
        $energyMeterRepository = app()->make(EnergyMeterRepository::class);
        $meterInfo = $energyMeterRepository->get($param['energy_id']);
        if (!$meterInfo) {
            return $this->error('设备不存在');
        }
        if ($meterInfo['is_bind'] == 1) {
            return $this->error('此设备已绑定');
        }
        if ($meterInfo['plan_id'] <= 0) {
            return $this->error('此设备未绑定计费方案');
        }
        $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId());
        return app('api_return')->success('添加成功');
    }

    public function edit(int $id)
    {
        $param = $this->request->param([
            'plan_name' => '',
            'plan_type' => '',
            'is_type' => '',
            'remark' => '',
        ]);
        validate(EnergySharedPlanValidate::class)->scene('edit')->check($param);
        $info = $this->repository->get($id);
        if (!$this->repository->existsByUserId($id, $this->request->userId())) {
            return $this->error('权限不足');
        }
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        $this->repository->editInfo($info, $param, $this->request->userId());
        return app('api_return')->success('更新成功');
    }

    /**
     *  方案详情
     * @param int $id
     * @return mixed
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-05-19 12:37
     */
    public function detail(int $id)
    {
        if (!$this->repository->existsByUserId($id, $this->request->userId())) {
            return $this->error('权限不足');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        return app('api_return')->success($info);
    }


    public function delete(int $id)
    {
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        if (!$this->repository->existsByUserId($id, $this->request->userId())) {
            return $this->error('权限不足');
        }
        $res = $this->repository->batchDelete([$id], $this->request->userId());
        if ($res) {
            return $this->successText('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }

}